import os, zipfile

# 待解压路径（重打包）
zippath = '..\\data\\repack'
# 解压目标路径
unzippath = '..\\data\\repack-unzip'

def unzip(f, t=None):
    """
    :param f: file path
    :param t: target dir
    :return:
    """
    if t is None:
        t = os.getcwd()
    t = t + "\\" if not t.endswith("\\") else t

    if not os.path.exists(t):
        raise Exception("解压目标位置不存在: ", t)

    try:
        file = zipfile.ZipFile(f)
        fn = f.split("\\")[-1].split(".")[0]
        print("unzipping :", fn)
        file.extractall(t + fn)
        file.close()
    except(zipfile.BadZipFile, zipfile.LargeZipFile) as e:
        print("Error:", f, str(e))


if __name__ == "__main__":
    for root, dirs, files in os.walk(zippath):
        for file in files:
            path = os.path.join(root, file)
            index = path.split('\\')[-2]
            unzip(os.path.join(root, file), unzippath)


